package com.gs.m_home.persistent

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.gs.common.constant.StateCode
import com.gs.m_home.callback.OnDoGetGoodsByQueryStateCallback
import com.gs.m_home.callback.OnDoGetGoodsStateCallback
import com.gs.m_home.model.ShoppingModel

class ShoppingPersistent(context: Context) {

    private val shoppingModel by lazy { ShoppingModel.getInstance(context) }
    private val handler = Handler(Looper.getMainLooper())

    fun getGoods(callback: OnDoGetGoodsStateCallback) {
        Thread{
            shoppingModel.getGoods {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetGoodsLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetGoodsSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetGoodsFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun getGoodsByQuery(query: String, callback: OnDoGetGoodsByQueryStateCallback) {
        Thread{
            shoppingModel.getGoodsByQuery(query) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetGoodsByQueryLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetGoodsByQuerySuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetGoodsByQueryFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    companion object{
        private var instance: ShoppingPersistent? = null
        fun getInstance(context: Context): ShoppingPersistent {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = ShoppingPersistent(context)
                    }
                }
            }
            return instance!!
        }
    }
}